﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MobileAPI.Entity.Model;
using System.Data.SqlClient;
using System.Data;
using MobileAPI.Common;
using MobileAPI.Entity;
using DALBase;
using System.Collections;

namespace MobileAPI.DAL
{
    public class DALRepairsInfo : BaseDAL
    {
        public DBAccess db = DBConnection.CreateDB();

        public ERepairsInfo GetRepairDetails(string RepairsId,string RepairsDate)
        {
            try
            {
                Hashtable hs = new Hashtable();
                hs.Add("用户名称", "");
                hs.Add("报修单号", RepairsId);
                hs.Add("客户名称", "");
                hs.Add("报修起日", "");
                hs.Add("报修截日", RepairsDate);
                hs.Add("处理进度", "");
                DataTable dt = db.ExecuteStoredProcQuery("SP_GQ_报修管理", hs);
                if (dt != null && dt.Rows.Count > 0)
                {
                    ERepairsInfo es = new ERepairsInfo();
                    es.SystemId = dt.Rows[0]["系统编号"].TInt32();
                    es.CustomerArea = dt.Rows[0]["客户区域"].TString();
                    es.CustomerName = dt.Rows[0]["客户名称"].TString();
                    es.Diagnosis = dt.Rows[0]["故障描述"].TString();
                    es.Flag = dt.Rows[0]["系统标志"].TInt32();
                    es.Instrument = dt.Rows[0]["仪器型号"].TString();
                    es.InstrumentID = dt.Rows[0]["仪器序号"].TString();
                    es.IsStop = dt.Rows[0]["是否停机"].TString();
                    es.Priority = dt.Rows[0]["紧迫程度"].TString();
                    es.RepairsDate = dt.Rows[0]["报修日期"].TString();
                    es.RepairsId = dt.Rows[0]["报修单号"].TString();
                    es.RepairsPhone = dt.Rows[0]["报修电话"].TString();
                    es.RepairsStaff = dt.Rows[0]["报修人员"].TString();
                    es.RoomName = dt.Rows[0]["科室名称"].TString();

                    EWorkOrder wo = new EWorkOrder();
                    wo.WorkOrderId = dt.Rows[0]["派工单号"].TString();
                    wo.WorkOrderStaff = dt.Rows[0]["派工人员"].TString();
                    wo.WorkOrderTime = dt.Rows[0]["派工时间"].TString();
                    wo.WorkOrderType = dt.Rows[0]["派工方式"].TString();
                    wo.ServiceType = dt.Rows[0]["服务类别"].TString();
                    wo.Assistant = dt.Rows[0]["协助人员"].TString();
                    wo.Remarks = dt.Rows[0]["派工说明"].TString();
                    wo.TechPhone = dt.Rows[0]["技术电话"].TString();

                    es.WorkOrder = wo;
                    es.RepairsStateList = (List<ERepairsState>)GetRepairsStatsList(RepairsId);

                    return es;
                }
                return null;
            }
            catch (Exception e)
            {
                base.WriteLog(e.Message);
                throw e;
            }
        }

        public IList<ERepairsState> GetRepairsStatsList(string RepairsId)
        {
            try
            {
                Hashtable hs = new Hashtable();
                hs.Add("报修单号", RepairsId);
                DataTable dt = db.ExecuteStoredProcQuery("SP_GM_报修状态", hs);
                IList<ERepairsState> list = null;
                if (dt != null && dt.Rows.Count > 0)
                {
                    list = new List<ERepairsState>();
                    ERepairsState rs = null;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        rs = new ERepairsState();
                        rs.SystemId = dt.Rows[i]["系统编号"].TInt32();
                        rs.WorkOrderId = dt.Rows[i]["派工单号"].TString();
                        rs.DisposeCondition = dt.Rows[i]["处理情况"].TString();
                        rs.OperatingDate = dt.Rows[i]["操作日期"].TString();
                        rs.OperatingStaff = dt.Rows[i]["操作人员"].TString();
                        rs.OperatingTime = dt.Rows[i]["操作时间"].TString();
                        rs.OperatingType = dt.Rows[i]["操作类别"].TString();
                        list.Add(rs);
                    }
                    return list;
                }
                return null;
            }
            catch (Exception e)
            {
                base.WriteLog(e.Message);
                throw e;
            }
        }

        public IList<ERepairsInfo> GetTodayRepairsList(string userName)
        {
            try
            {
                Hashtable hs = new Hashtable();
                hs.Add("用户名称", userName);
                DataTable dt = db.ExecuteStoredProcQuery("SP_GM_当日报修", hs);
                IList<ERepairsInfo> list = null;
                if (dt != null && dt.Rows.Count > 0)
                {
                    list= new List<ERepairsInfo>();
                    ERepairsInfo es = null;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        es = new ERepairsInfo();
                        es.RepairsId = dt.Rows[i]["报修单号"].TString();
                        es.CustomerName = dt.Rows[i]["客户名称"].TString();
                        es.Diagnosis = dt.Rows[i]["故障描述"].TString();
                        es.Instrument = dt.Rows[i]["仪器型号"].TString();
                        es.Progress = dt.Rows[i]["处理进度"].TString();
                        list.Add(es);
                    }

                    return list;
                }
                return null;
            }
            catch (Exception e)
            {
                base.WriteLog(e.Message);
                throw e;
            }
        }

        public IList<ERepairsInfo> SearchTodayRepairsList(string UserName,string CustomerName,string RepairsID,string StartDate,string EndDate,string Progress)
        {
            try
            {
                Hashtable hs = new Hashtable();
                hs.Add("用户名称", UserName);
                hs.Add("客户名称", CustomerName);
                hs.Add("报修单号", RepairsID);
                hs.Add("报修起日", StartDate);
                hs.Add("报修截日", EndDate);
                hs.Add("处理进度", Progress);

                DataTable dt = db.ExecuteStoredProcQuery("SP_GQ_报修管理", hs);
                IList<ERepairsInfo> list = null; 
                if (dt != null && dt.Rows.Count > 0)
                {   
                    list = new List<ERepairsInfo>();
                    ERepairsInfo es = null;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        es = new ERepairsInfo();
                        es.RepairsId = dt.Rows[i]["报修单号"].TString();
                        es.CustomerName = dt.Rows[i]["客户名称"].TString();
                        es.Diagnosis = dt.Rows[i]["故障描述"].TString();
                        es.Instrument = dt.Rows[i]["仪器型号"].TString();
                        es.Progress = dt.Rows[i]["处理进度"].TString();
                        list.Add(es);
                    }

                    return list;
                }
                return null;
            }
            catch (Exception e)
            {
                base.WriteLog(e.Message);
                throw e;
            }
        }

        public bool InsertRepairs(ERepairsInfo repair, string UserName)
        {
            try
            {
                Hashtable hs = new Hashtable();
                hs.Add("用户名称", UserName);
                hs.Add("客户区域", repair.CustomerArea);
                hs.Add("客户名称", repair.CustomerName);
                hs.Add("科室名称", repair.RoomName);
                hs.Add("仪器型号", repair.Instrument);
                hs.Add("仪器序号", repair.InstrumentID);
                hs.Add("报修日期", repair.RepairsDate);
                hs.Add("报修人员", repair.RepairsStaff);
                hs.Add("报修电话", repair.RepairsPhone);
                hs.Add("紧迫程度", repair.Priority);
                hs.Add("是否停机", repair.IsStop);
                hs.Add("故障描述", repair.Diagnosis);
                db.BeginTran();
                bool IsSuccess=db.ExecuteStoredProcedure("SP_AM_报修信息", hs);
                db.CommitTran();
                return IsSuccess;
            }
            catch (Exception e)
            {
                db.RollbackTran();
                base.WriteLog(e.Message);
                throw e;
            }
        }
    }
}
